package com.hhd.form.component;

import com.hhd.form.entity.DesignFormFieldDefinition;
import com.hhd.form.entity.DesignFormTableFieldMeta;
import com.hhd.form.entity.DesignFormTableMeta;
import com.hhd.form.table.field.TableFieldDefinition;
import com.hhd.form.vo.FormDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FieldComponentDelegate {

    @Autowired
    List<FieldComponent> components;

    public List<TableFieldDefinition> onCreateField(DesignFormFieldDefinition field, DesignFormTableFieldMeta fieldMeta, FormDefinition fomDefinition, DesignFormTableMeta tableMeta) {
        return getComponent(field.getComponent()).createField(field, fieldMeta, fomDefinition, tableMeta);
    }

    public FieldComponent getComponent(String componentName) {
        for (FieldComponent component : components) {
            if (component.getName().equals(componentName)) {
                return component;
            }
        }
        throw new RuntimeException("不支持的后端组件: " + componentName);
    }
}
